{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c0e8c3e1",
   "metadata": {},
   "source": [
    "# 本周知识点\n",
    "## 1. 字典的初始 （{key:value}）\n",
    "### 1.字典的文档\n",
    "### 2.字典的重要属性：\n",
    " 结构：key:value（键值对）\n",
    " 1.字典是无序的，我们通过键(key)去查找值(value)，就好像list用索引(index)查找值(value)一样，只不过字典的“索引”被定义了名字\n",
    " 2.字典对key的定义有一定的要求：key是由\"不可变\"的数据结构才可以定义，list是不能作为key来定义的。\n",
    " 3.字典的key是唯一的，字典的值value也可以不唯一。（list的值是唯一的么？）\n",
    "  4.字典的创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4c8be556",
   "metadata": {},
   "outputs": [],
   "source": [
    "users = [\n",
    "    [\"yuanlin\",20,\"IT\",2000],\n",
    "    [\"du\",19,\"IT\",3000]\n",
    "    \n",
    "]\n",
    "# 试对比：\n",
    "users_dict = {\n",
    "    \"yuanlin\":[20,\"IT\",2000],\n",
    "    \"du\":[19,\"IT\",3000]\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6071dd74",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['du', 19, 'IT', 3000]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询qian的信息：\n",
    "users[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8306cc4c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[19, 'IT', 3000]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "users_dict[\"du\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76b5fa70",
   "metadata": {},
   "source": [
    "试问：如果users的数据很大，我想查其中的一个信息，但无法知道其索引值\n",
    "list就变得很无力，\n",
    "而dict只需要知道数据的关键信息即可"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbbc1ea5",
   "metadata": {},
   "source": [
    "### 2. 字典的创建(常见的三种创建方式)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11b9c6cc",
   "metadata": {},
   "source": [
    "### 1.info = {key1:value1 , key2:value2 , ...}直接按照字典格式创建\n",
    "### 2.info_02 = dict(key1=value1 , key2=value2 , ... ) 使用dict()进行字典创建\n",
    "### 3.运用字典增加的方式新建字典:\n",
    "### info_03 = {} info_03[key1] = value1 ,info_03[key2] = value2 ... "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "47815e4a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'du', 'stu10002': 'yuan', 'stu10003': 'lin'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info = {\n",
    "    \"stu10001\":\"du\",\n",
    "    \"stu10002\":\"yuan\",\n",
    "    \"stu10003\":\"lin\",\n",
    "#     \"stu10004\":\"lin\",\n",
    "}\n",
    "print(info)\n",
    "print(type(info))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "59e33add",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'du', 'stu10002': 'yuan', 'stu10003': 'lin'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info_02 = dict(\n",
    "    stu10001=\"du\",\n",
    "    stu10002=\"yuan\",\n",
    "    stu10003=\"lin\",\n",
    ")\n",
    "print(info_02)\n",
    "print(type(info_02))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1d5b0379",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'du', 'stu10002': 'yuan', 'stu10003': 'lin'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info_03 = {} # 新建空字典\n",
    "info_03[\"stu10001\"] = \"du\"\n",
    "info_03[\"stu10002\"] = \"yuan\"\n",
    "info_03[\"stu10003\"] = \"lin\"\n",
    "print(info_03)\n",
    "print(type(info_03))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aec748c6",
   "metadata": {},
   "source": [
    "# 字典的增、删、改、查\n",
    "## 1.字典的增加(和字典的第三种创建方式一致)：\n",
    " * info_03[\"stu10004\"] = \"zhichao\"\n",
    "## 字典的查询\n",
    " * info_03[key_name]\n",
    " * 注意： 正确的查询方式，应该先查询key是否存在字典当中，如下：\n",
    "   1.key_name in info_03 ## 查看是否存在,如果存在，返回结果为 True ，不存在为 False\n",
    "   2.info_03[key_name]\n",
    "## 字典的修改\n",
    "### 1.查询key是否存在\n",
    "### 2.修改key的值\n",
    " * 例如： 修改info_03中stu10003的值为Alex, if \"stu10003\" in info_03 : info_03 [\"stu10003\"] = \"Alex\"\n",
    "## 字典的删除\n",
    "### 1.pop(key) 2.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8213a5a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'du', 'stu10002': 'yuan', 'stu10003': 'lin', 'stu10004': 'yuanlin'}\n"
     ]
    }
   ],
   "source": [
    "info_03 [\"stu10004\"] = \"yuanlin\"\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b87345f0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'lin'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用key查到value的数据\n",
    "info_03[\"stu10003\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1caec20c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "查询的key不存在\n"
     ]
    }
   ],
   "source": [
    "# 正确的查询字典方式\n",
    "print(\"stu10003\" in info_03)\n",
    "if \"stu10006\" in info_03:\n",
    "    print(info_03[\"stu10006\"])\n",
    "else:\n",
    "    print(\"查询的key不存在\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "51d15d26",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'du', 'stu10002': 'yuan', 'stu10003': 'apex', 'stu10004': 'yuanlin'}\n"
     ]
    }
   ],
   "source": [
    "# 修改: 也要先查询key\n",
    "if \"stu10003\" in info_03:\n",
    "    info_03 [\"stu10003\"] = \"apex\"\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "6044541e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'du', 'stu10002': 'yuan', 'stu10004': 'yuanlin'}\n"
     ]
    }
   ],
   "source": [
    "# 字典的删除\n",
    "info_03.pop(\"stu10003\")\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "9187b564",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'du', 'stu10002': 'yuan'}\n"
     ]
    }
   ],
   "source": [
    "# 字典的删除 popitem()\n",
    "info_03.popitem()\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df3042d2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
